/**
 * @author:albert
 */

package com.bkbw.live.common.net.codec;

import java.io.ByteArrayOutputStream;
import java.lang.reflect.Method;

import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolEncoderAdapter;
import org.apache.mina.filter.codec.ProtocolEncoderOutput;

import com.bkbw.live.battle.net.codec.PackageHead;

public class NetEncoder extends ProtocolEncoderAdapter {
	public void encode(IoSession session, Object message, ProtocolEncoderOutput out) throws Exception {
//    	byte[] data = (byte[]) message;
//    	int capacity = data.length + 4;
//    	IoBuffer buffer = IoBuffer.allocate(capacity, false);
//    	// package len
//    	buffer.putInt(data.length);
//    	buffer.put(data);
//    	buffer.flip();
//        out.write(buffer);
    	
    	RawMessageObject rawMessageObject = (RawMessageObject)message;
    	int capacity = rawMessageObject.getData().length + 6;//4用于写入包长  2用于写入消息头
    	IoBuffer buffer = IoBuffer.allocate(capacity, false);
    	// put package length
    	buffer.putInt(rawMessageObject.getData().length + 2);
    	// put head
    	buffer.putShort(rawMessageObject.getHead());
    	// put data
    	buffer.put(rawMessageObject.getData());
    	buffer.flip();
        out.write(buffer);
    
    }
}
